package top.psjj.mall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.psjj.mall.comment.Constant;
import top.psjj.mall.comment.Result;
import top.psjj.mall.exception.MallException;
import top.psjj.mall.exception.MallExceptionEnum;
import top.psjj.mall.model.po.User;
import top.psjj.mall.service.api.UserService;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //前台注册
    @PostMapping("/register")
    public Result register(
            @RequestParam("userName") String username,
            @RequestParam("password") String password){
        if(!StringUtils.hasText(username)){
            return Result.error(MallExceptionEnum.NEED_USERNAME);
        }
        if(!StringUtils.hasText(password)){
            return Result.error(MallExceptionEnum.NEED_PASSWORD);
        }
        if(password.length()<6){
            return Result.error(MallExceptionEnum.PASSWORD_LEN_SHORT);
        }
        userService.register(username,password);
        return Result.success();
    }
    //前台登陆
    @PostMapping("/login")
    public Result login(
            @RequestParam("userName") String username,
            @RequestParam("password") String password,
            HttpSession session){
        //1.校验
        if(!StringUtils.hasText(username)){
            return Result.error(MallExceptionEnum.NEED_USERNAME);
        }
        if(!StringUtils.hasText(password)){
            return Result.error(MallExceptionEnum.NEED_PASSWORD);
        }
        if(password.length()<6){
            return Result.error(MallExceptionEnum.PASSWORD_LEN_SHORT);
        }
        //2.登陆
        User user = userService.login(username,password);
        //防止密码污染
        user.setPassword(null);
        //3.将用户信息存入回话
        session.setAttribute(Constant.User.CURRENT_USER,user);
        //4.响应结果
        return Result.success(user);
    }
    //修改签名前后台通用
    @PostMapping("/user/update")
    public Result updateSignature(
            @RequestParam("signature") String signature ,
            HttpSession session){
        //1.从回话中取用户信息
        User user = (User)session.getAttribute(Constant.User.CURRENT_USER);
        if(user==null){
            throw new MallException(MallExceptionEnum.NEED_LOGIN);
        }
        user.setPersonalizedSignature(signature);
        //2.将签名修改至数据库
        userService.updateUser(user);
        return Result.success();
    }
    //安全退出 前后台通用
    @PostMapping("/user/logout")
    public Result logout(HttpSession session){
        session.invalidate();
        return Result.success();
    }
    //后台登陆
    @PostMapping("/adminLogin")
    public Result adminLogin(
            @RequestParam("userName") String username,
            @RequestParam("password") String password,
            HttpSession session){
        //1.校验
        if(!StringUtils.hasText(username)){
            return Result.error(MallExceptionEnum.NEED_USERNAME);
        }
        if(!StringUtils.hasText(password)){
            return Result.error(MallExceptionEnum.NEED_PASSWORD);
        }
        if(password.length()<6){
            return Result.error(MallExceptionEnum.PASSWORD_LEN_SHORT);
        }
        //2.登陆
        User user = userService.login(username,password);
        //判断是否是管理员身份
        if(!user.getRole().equals(Constant.User.IS_ADMIN)){
            throw new MallException(MallExceptionEnum.NEED_ADMIN);
        }
        //防止密码污染
        user.setPassword(null);
        //3.将用户信息存入回话
        session.setAttribute(Constant.User.CURRENT_USER,user);
        //4.响应结果
        return Result.success(user);
    }
}
